package com.offcn.web;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.offcn.entity.User;
import com.offcn.service.UserService;
import com.offcn.service.UserServiceImpl;
import com.offcn.utlis.BaseServlet;
import com.offcn.utlis.PageUtils;
import com.offcn.utlis.ResultVo;
import com.offcn.utlis.VerifyCodeUtils;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

@WebServlet("/user")
public class LoginServlet extends BaseServlet {
    // 调用业务层
    UserService userService = new UserServiceImpl();
    // 调用验证工具
    VerifyCodeUtils codeUtils = new VerifyCodeUtils();
    // 调用状态工具
    ResultVo resultVo = null;
    //使用sjon
    ObjectMapper objectMapper = new ObjectMapper();



    // 登陆
    public void  login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String imageCode = req.getParameter("imageCode");
        User user = userService.loginUser(username, password);
        HttpSession session = req.getSession();
        String verCode = (String) session.getAttribute("verCode");

        //先判断验证码是否正确
        if (verCode.equalsIgnoreCase(imageCode)){
            // 判断数据库中的账号和密码是否有
            if (user !=null){
                // 判断是否是管理员
                if (user.getRole()==1){
                    resultVo = new ResultVo(0,"登陆成功",user);
                }else {
                    resultVo = new ResultVo(1,"权限不足",null);
                }
            }else {
                resultVo = new ResultVo(3, "密码或账号错误", null);
            }
        }else {
            resultVo = new ResultVo(2,"验证码有误",null);
        }
        resp.getWriter().println(objectMapper.writeValueAsString(resultVo));
        req.getSession().setAttribute("user",user.getName());
    }

    // 回写名字
    public void getName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        String user = (String) session.getAttribute("user");
        resultVo = new ResultVo(200,"数据回传成功",user);
        resp.getWriter().println(objectMapper.writeValueAsString(resultVo));
    }

    // 退出
    public void  indexdele(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        session.removeAttribute("user");
        resultVo = new ResultVo(200,"注销成功...",null);
        resp.getWriter().println(objectMapper.writeValueAsString(resultVo));
    }

    // 添加
    public  void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
                User  user = new User();
        BeanUtils.populate(user,req.getParameterMap());
            boolean b  =userService.add(user);
            if (b){
                resultVo =new ResultVo(200,"添加成功...",null);
            }else {
                resultVo = new ResultVo(500,"添加失败...",null);
            }
            resp.getWriter().println(objectMapper.writeValueAsString(resultVo));
    }

    // 分页 查询
    public void show(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String search = req.getParameter("search1"); //查询条件
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));//每页显示的记录数
        int currentPage = Integer.parseInt(req.getParameter("currentPage"));//当前页码
        // 接收数据回数据库查询
         PageUtils<User> userPageUtils = userService.findByAll(search,pageSize,currentPage );
         // 回传数据
         resp.getWriter().println(objectMapper.writeValueAsString(userPageUtils));

    }

    // 修改
    public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
            User user = new User();
            BeanUtils.populate(user,req.getParameterMap());
          boolean b = userService.updateUser(user);
          if (b){
              resultVo = new ResultVo(200,"修改成功！",null);
          }else {
              resultVo = new ResultVo(500,"修改失败",null);
          }
          resp.getWriter().println(objectMapper.writeValueAsString(resultVo));
    }

    //删除
    public void del(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String[] ids = req.getParameterValues("uid");
               boolean b  = userService.del(ids);
               if (b){
                   resultVo = new ResultVo(200,"删除成功",null);
               }else {
                   resultVo = new ResultVo(500,"删除失败",null);
               }
               resp.getWriter().println(objectMapper.writeValueAsString(resultVo));
    }
}
